var messagesLength = 0;
$(document).ready(
        function() {
                $("#form1").hide();
				$("#form2").show();
                $("#form3").hide();

                $("#a").click(function() {
                        $("#form2").hide();
                        $("#form1").show();
                        $("#form3").hide();
                });
                $("#b").click(function() {
                        $("#form1").hide();
                        $("#form2").show();
                        $("#form3").hide();
                });
                
                $('#search').keypress(function(event){
                        var keycode = (event.keyCode ? event.keyCode : event.which);
                        var id = document.getElementById("searching").value;
                        if(keycode == '13'){
                                $.getJSON('services/manager/friendProfile/'+id, function(detail) {
                                        if (detail.emailAddress === null) {
                                                alert("User tidak ditemukan.");
                                        } else {
                                                alert(detail.emailAddress);
                                        }
                                });
                        }
                });
                
                $.getJSON('services/manager/messages', function(data) {
                        $.each(data.threads, function(key, val) {
                                var liElement = document.createElement('li');
                                var checkBox = document.createElement('input');
                                var aElement = document.createElement('a');
                                var participant = document.createElement('span');
                                checkBox.type="checkbox";
                                checkBox.value=val.id;
                                aElement.href="#"+val.id;
                                aElement.textContent=val.subject;
                                aElement.setAttribute("style","padding: 10px !important");
                                participant.textContent=val.participants.toString();
                                liElement.appendChild(checkBox);
                                liElement.appendChild(aElement);
                                liElement.appendChild(participant);
                                liElement.setAttribute("style","float:none !important; padding-bottom: 5px;");
                                var ulElement = $("#form2 ul").append(liElement);
                                $("#form2 ul li a").click(function(){
                                        var path = this.href;
                                        var id = path.substr(path.indexOf('#')+1, path.length);
                                        $.getJSON('services/manager/messages/'+id, function(detail) {
                                                        $("#messageDetail thead tr").children().remove();
                                                        $("#messageDetail thead tr").append("<th>"+detail.threads[0].subject+"</th>");
                                                        $.each(detail.threads[0].messages, function(detailkey, detailval) {
                                                                $("#messageDetail tbody").children().remove();
                                                                var detailRowSummary = document.createElement('tr');
                                                                var detailValueSummaryLabel = document.createElement('td');
                                                                var detailValueSummaryValue = document.createElement('td');
                                                                detailValueSummaryLabel.textContent="Subject";
                                                                detailValueSummaryValue.textContent=detail.threads[0].subject;
                                                                detailRowSummary.appendChild(detailValueSummaryLabel);
                                                                detailRowSummary.appendChild(detailValueSummaryValue);
                                                                $("#messageDetail tbody").append(detailRowSummary);
                                                                var detailRowContent = document.createElement('tr');
                                                                var detailValueContent = document.createElement('td');
                                                                detailValueContent.colSpan=2;
                                                                detailValueContent.textContent=detailval.description;
                                                                detailRowContent.appendChild(detailValueContent);
                                                                $("#messageDetail tbody").append(detailRowContent);
                                                        });
                                                $("#form3").show();
                                                $("#form1").hide();
                                                $("#form2").hide();
                                        });
                                });
                        });
                });
});